﻿using GameData.Domains.Mod;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Reflection;
using TaiwuModFramework.Common;
using TaiwuModFramework.Common.Abstract;
using TaiwuModFramework.Common.Base;
using TaiwuModFramework.Common.Manager;

namespace TaiwuModFramework.Frontend.Comp {
    internal class CallModMethodComp : BaseTaiwuComp {
        private static ALogger Log;
        private static Dictionary<string, ModInfo> modInfos = null;

        private static void CheckModInfo() {
            if(modInfos == null) {
                modInfos = (Dictionary<string, ModInfo>)typeof(ModManager).GetField("_localMods", (BindingFlags)(-1)).GetValue(null); ;
            }
        }

        [HarmonyPostfix]
        [HarmonyPatch(typeof(ModManager), "UpdateModSettingsInGame")]
        public static void ModManager_UpdateModSettingsInGame_Postfix(ModId modId) {
            CheckModInfo();
            try {
                ConfigManager.UpdateItem(modId.ToString());
            } catch(Exception e) {
                Log.Error(e);
            }
        }
    }
}
